package com.azt.novel.home.manager;


import com.azt.novel.common.constant.CacheConsts;
import com.azt.novel.home.dao.entity.HomeFriendLink;
import com.azt.novel.home.dao.mapper.HomeFriendLinkMapper;
import com.azt.novel.home.resp.HomeFriendLinkRespDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class FriendLinkCacheManager {

    @Resource
    private HomeFriendLinkMapper homeFriendLinkMapper;

    @Cacheable(cacheManager = CacheConsts.REDIS_CACHE_MANAGER,
                      value = CacheConsts.HOME_FRIEND_LINK_CACHE_NAME)
    public List<HomeFriendLinkRespDto> listHomeFriendLinks(){
        LambdaQueryWrapper<HomeFriendLink> homeFriendLinkLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        homeFriendLinkLambdaQueryWrapper.orderByAsc(HomeFriendLink::getSort);
        List<HomeFriendLink> homeFriendLinks = homeFriendLinkMapper.selectList(homeFriendLinkLambdaQueryWrapper);
        List<HomeFriendLinkRespDto> homeFriendLinkRespDtos = new ArrayList<>();
        homeFriendLinks.forEach(homeFriendLink -> {
            HomeFriendLinkRespDto homeFriendLinkRespDto = HomeFriendLinkRespDto.builder()
                    .linkName(homeFriendLink.getLinkName())
                    .linkUrl(homeFriendLink.getLinkUrl())
                    .build();
            homeFriendLinkRespDtos.add(homeFriendLinkRespDto);
        });
        return homeFriendLinkRespDtos;
    }
}
